<template>
  <div class="login-container">
    <div class="background-bubbles">
      <div v-for="n in 10" :key="`bg-${n}`" class="bubble"></div>
    </div>
    <div class="login-card">

      <div class="login-left">
        <div class="illustration-bubbles">
          <div v-for="n in 15" :key="`ill-${n}`" class="illustration-bubble"></div>
        </div>
      </div>
      <div class="login-right">
        <h2 class="welcome-text">欢迎登录</h2>
        <el-form
            ref="formRef"
            :model="loginForm"
            :rules="rules"
            class="login-form"
        >
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                :prefix-icon="User"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
            />
          </el-form-item>

          <el-form-item prop="code">
            <div class="verification-code-input">
              <el-input
                  v-model="loginForm.code"
                  placeholder="请输入验证码"
                  :prefix-icon="Key"
                  @keyup.enter="handleLogin"
              />
              <div>
                <el-image class="verification-code cursor-pointer" :src="captchaImage" @click="refreshCaptcha"></el-image>
              </div>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button
                type="primary"
                class="login-button"
                :loading="loading"
                @click="handleLogin"
            >
              登 录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive, onMounted} from 'vue'
import type {FormInstance, FormRules} from 'element-plus'
import {User, Lock, Key} from '@element-plus/icons-vue'
import { getCaptcha } from "@/api/auth";
import { useAuthStore } from '@/stores/auth';
import { useRouter } from 'vue-router';
import { mountDynamicRoutes } from '@/router'

const authStore = useAuthStore();
const router = useRouter();

interface LoginForm {
  username: string
  password: string
  code: string
}

const formRef = ref<FormInstance>();
const loading = ref(false);
const captchaImage = ref('');

const loginForm = reactive<LoginForm>({
  username: '',
  password: '',
  code: ''
})

const rules: FormRules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'}
  ],
  code: [
    {required: true, message: '请输入验证码', trigger: 'blur'}
  ]
}

// 刷新验证码
const refreshCaptcha = async () => {
  const response = await getCaptcha();
  captchaImage.value = response.data.captcha;
}

// 登录事件
const handleLogin = async () => {
  if (!formRef.value) return

  await formRef.value.validate()

  try {
    loading.value = true
    await authStore.login(loginForm);
    mountDynamicRoutes(); // 挂载动态路由
    await router.push('/');
  } catch (error) {
    loading.value = false
    await refreshCaptcha()
  }
}

onMounted(() => {
  refreshCaptcha();
})
</script>

<style scoped>
.login-container {
  position: relative;
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--el-color-primary-light-8) 0%, var(--el-color-primary) 100%);
  overflow: hidden;
}

.login-card {
  display: flex;
  background: white;
  border-radius: 16px;
  overflow: hidden;
  width: 900px;
  height: 500px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  position: relative;
  z-index: 1;
}

.login-left {
  flex: 1;
  background: var(--el-color-primary);
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.logo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  margin-bottom: 40px;
  z-index: 2;
}

.logo-icon {
  font-size: 48px;
}

.logo-text {
  font-size: 24px;
  font-weight: 600;
  text-align: center;
}

.illustration-bubbles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.illustration-bubble {
  position: absolute;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  animation: float 8s infinite ease-in-out;
}

.login-right {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.welcome-text {
  font-size: 24px;
  font-weight: 600;
  color: #1F2937;
  margin-bottom: 40px;
}

.login-form {
  flex: 1;
}

.verification-code-input {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  height: 34px;
}

.verification-code {
  width: 100%;
  height: 100%;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05) !important;
  border: 1px solid #E5E7EB;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: var(--el-color-primary);
  box-shadow: 0 0 0 1px var(--el-color-primary) !important;
}

.login-button {
  width: 100%;
  height: 40px;
  background: var(--el-color-primary);
  border-color: var(--el-color-primary);
  font-size: 16px;
}

.background-bubbles {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 0;
  overflow: hidden;
  top: 0;
  left: 0;
}

.bubble {
  position: absolute;
  bottom: -100px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  opacity: 0.5;
  animation: rise 10s infinite ease-in;
}

@keyframes rise {
  0% {
    bottom: -100px;
    transform: translateX(0);
  }
  50% {
    transform: translate(100px, -500px);
  }
  100% {
    bottom: 1080px;
    transform: translateX(-200px);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-20px);
  }
}

.illustration-bubble:nth-child(1) {
  left: 10%;
  top: 10%;
  width: 60px;
  height: 60px;
  animation-delay: 0s;
}

.illustration-bubble:nth-child(2) {
  left: 20%;
  top: 40%;
  width: 40px;
  height: 40px;
  animation-delay: 1s;
}

.illustration-bubble:nth-child(3) {
  left: 35%;
  top: 60%;
  width: 50px;
  height: 50px;
  animation-delay: 2s;
}

.illustration-bubble:nth-child(4) {
  left: 50%;
  top: 20%;
  width: 30px;
  height: 30px;
  animation-delay: 3s;
}

.illustration-bubble:nth-child(5) {
  left: 65%;
  top: 70%;
  width: 45px;
  height: 45px;
  animation-delay: 4s;
}

.illustration-bubble:nth-child(6) {
  left: 80%;
  top: 30%;
  width: 55px;
  height: 55px;
  animation-delay: 5s;
}

.illustration-bubble:nth-child(7) {
  left: 15%;
  top: 80%;
  width: 35px;
  height: 35px;
  animation-delay: 6s;
}

.illustration-bubble:nth-child(8) {
  left: 75%;
  top: 50%;
  width: 40px;
  height: 40px;
  animation-delay: 7s;
}

.illustration-bubble:nth-child(9) {
  left: 90%;
  top: 10%;
  width: 50px;
  height: 50px;
  animation-delay: 8s;
}

.illustration-bubble:nth-child(10) {
  left: 5%;
  top: 50%;
  width: 45px;
  height: 45px;
  animation-delay: 9s;
}

.illustration-bubble:nth-child(11) {
  left: 40%;
  top: 90%;
  width: 55px;
  height: 55px;
  animation-delay: 10s;
}

.illustration-bubble:nth-child(12) {
  left: 60%;
  top: 5%;
  width: 40px;
  height: 40px;
  animation-delay: 11s;
}

.illustration-bubble:nth-child(13) {
  left: 85%;
  top: 80%;
  width: 35px;
  height: 35px;
  animation-delay: 12s;
}

.illustration-bubble:nth-child(14) {
  left: 25%;
  top: 25%;
  width: 50px;
  height: 50px;
  animation-delay: 13s;
}

.illustration-bubble:nth-child(15) {
  left: 70%;
  top: 95%;
  width: 45px;
  height: 45px;
  animation-delay: 14s;
}

.bubble:nth-child(1) {
  width: 40px;
  height: 40px;
  left: 10%;
  animation-duration: 8s;
}

.bubble:nth-child(2) {
  width: 20px;
  height: 20px;
  left: 20%;
  animation-duration: 5s;
  animation-delay: 1s;
}

.bubble:nth-child(3) {
  width: 50px;
  height: 50px;
  left: 35%;
  animation-duration: 7s;
  animation-delay: 2s;
}

.bubble:nth-child(4) {
  width: 80px;
  height: 80px;
  left: 50%;
  animation-duration: 11s;
  animation-delay: 0s;
}

.bubble:nth-child(5) {
  width: 35px;
  height: 35px;
  left: 55%;
  animation-duration: 6s;
  animation-delay: 1s;
}

.bubble:nth-child(6) {
  width: 45px;
  height: 45px;
  left: 65%;
  animation-duration: 8s;
  animation-delay: 3s;
}

.bubble:nth-child(7) {
  width: 90px;
  height: 90px;
  left: 70%;
  animation-duration: 12s;
  animation-delay: 2s;
}

.bubble:nth-child(8) {
  width: 25px;
  height: 25px;
  left: 80%;
  animation-duration: 6s;
  animation-delay: 2s;
}

.bubble:nth-child(9) {
  width: 15px;
  height: 15px;
  left: 70%;
  animation-duration: 5s;
  animation-delay: 1s;
}

.bubble:nth-child(10) {
  width: 90px;
  height: 90px;
  left: 25%;
  animation-duration: 10s;
  animation-delay: 4s;
}

@media (max-width: 768px) {
  .login-card {
    width: 95%;
    height: auto;
    max-height: 90vh;
    flex-direction: column;
  }

  .login-left {
    padding: 20px;
    height: 150px;
  }

  .login-right {
    padding: 20px;
  }

  .logo-section {
    margin-bottom: 0;
  }

  .logo-icon {
    font-size: 36px;
  }

  .logo-text {
    font-size: 18px;
  }
}
</style>
  
  